"""配置文件"""
import os
from dotenv import load_dotenv

load_dotenv()

# OpenAI配置
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
OPENAI_BASE_URL = os.getenv("OPENAI_BASE_URL")

# 验证并修复 base_url 格式
if OPENAI_BASE_URL:
    # 确保 base_url 包含协议前缀
    if not OPENAI_BASE_URL.startswith(("http://", "https://")):
        # 如果没有协议前缀，默认使用 https://
        OPENAI_BASE_URL = f"https://{OPENAI_BASE_URL}"
        print(f"⚠️  警告: OPENAI_BASE_URL 缺少协议前缀，已自动添加为: {OPENAI_BASE_URL}")
    
    # 移除末尾的斜杠和常见的API路径（防止路径重复）
    OPENAI_BASE_URL = OPENAI_BASE_URL.rstrip("/")
    
    # 如果包含 /v1/chat/completions 或其他完整路径，只保留到 /v1
    if "/chat/completions" in OPENAI_BASE_URL:
        OPENAI_BASE_URL = OPENAI_BASE_URL.split("/chat/completions")[0]
    elif "/v1" in OPENAI_BASE_URL and not OPENAI_BASE_URL.endswith("/v1"):
        # 如果已经有 /v1 但不是结尾，则截取到 /v1
        OPENAI_BASE_URL = OPENAI_BASE_URL.split("/v1")[0] + "/v1"

# 模型配置
DEFAULT_MODEL = os.getenv("DEFAULT_MODEL", "claude-3-5-sonnet-20241022")

# 环境配置
ENV = os.getenv("ENV", "development").lower()  # development 或 production

# 后端API配置（系统 API 服务器地址）
# 开发环境：http://localhost:5080/
# 生产环境：https://ecgs.fowep.com/prod-api
# 可通过环境变量 BACKEND_API_BASE_URL 覆盖
if ENV == "production":
    BACKEND_API_BASE_URL = os.getenv("BACKEND_API_BASE_URL", "https://ecgs.fowep.com/prod-api")
else:
    BACKEND_API_BASE_URL = os.getenv("BACKEND_API_BASE_URL", "http://localhost:5080")

# 服务器配置
SERVER_PORT = int(os.getenv("SERVER_PORT", "9999"))
SERVER_HOST = os.getenv("SERVER_HOST", "0.0.0.0")

# 知识库配置
KB_ENABLED = os.getenv("KB_ENABLED", "true").lower() == "true"
KB_PATH = os.getenv("KB_PATH", "../knowledge_base")
KB_VECTORSTORE_PATH = os.getenv("KB_VECTORSTORE_PATH", "../vectorstore")

# 阿里云文本嵌入配置（text-embedding-v4）
ALIYUN_API_KEY = os.getenv("DASHSCOPE_API_KEY")
ALIYUN_API_ENDPOINT = os.getenv("ALIYUN_API_ENDPOINT", "https://dashscope.aliyuncs.com/api/v1/services/embeddings/text-embedding/text-embedding-v4")
KB_EMBEDDING_CHUNK_SIZE = int(os.getenv("KB_EMBEDDING_CHUNK_SIZE", "1000"))
KB_EMBEDDING_CHUNK_OVERLAP = int(os.getenv("KB_EMBEDDING_CHUNK_OVERLAP", "200"))

# MySQL 数据库配置
DB_HOST = os.getenv("DB_HOST", "localhost")
DB_PORT = int(os.getenv("DB_PORT", "3306"))
DB_USER = os.getenv("DB_USER", "root")
DB_PASSWORD = os.getenv("DB_PASSWORD", "")
DB_NAME = os.getenv("DB_NAME", "my_agent")
DB_CHARSET = os.getenv("DB_CHARSET", "utf8mb4")

